<?php
/* @var $this \yii\web\View */
/* @var $model */
/* @var $permissionList */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$Permission = $this->context->permission;
$this->title = $Permission->description . '<small>'.$model->title.'</small>';
?>
<div class="row">
    <div class="box">
        <div class="box-body">
<?php
$form = ActiveForm::begin(['layout'=>'default']);
/** @var backend\models\Permission $permission_model */
echo "<div class='form-group'>";
echo "<label>选择权限</label>";
foreach ($permissionList as $permission_model) {
    echo '<dl>';
    $checkbox = Html::checkbox(Html::getInputName($model,'permissions[]'),!empty($model->permissions) && in_array($permission_model->id,$model->permissions),['label'=>$permission_model->description,'value'=>$permission_model->id,'labelOptions'=>['class'=>'checkbox-inline']]);
    echo Html::tag('dt',$checkbox);
    if ($children = $permission_model->children) {
        foreach ($children as $child_permission_model) {
            $checkbox = Html::checkbox(Html::getInputName($model,'permissions[]'),!empty($model->permissions) && in_array($child_permission_model->id,$model->permissions),['label'=>$child_permission_model->description,'value'=>$child_permission_model->id]);
            echo Html::tag('dd',$checkbox,['class'=>'checkbox','style'=>'padding-left:20px;']);
            echo '<dd>';
            if ($grands = $child_permission_model->children) {
                $items = \yii\helpers\ArrayHelper::map($grands,'id','description');
                echo Html::checkboxList(Html::getInputName($model,'permissions[]'),$model->permissions,$items,[
                    'item'=>function ($index, $label, $name, $checked, $value) {
                        return '<li class="checkbox">' . \yii\helpers\Html::checkbox($name, $checked, ['label' => $label, 'value' => $value]) . '</li>';
                    },
                    'class'=>'list-inline',
                    'tag'=>'ul',
                    'style'=>'padding-left:20px'
                ]);
            }
            echo '</dd>';
        }
    }
    echo '</dl>';
}
echo '</div>';
?>
<div class="form-group">
    <div class="col-sm-12">
        <?= \yii\helpers\Html::submitButton('确认', ['class' => 'btn btn-primary']) ?>
        <a href="index" class="btn btn-primary">取消</a>
    </div>
</div>
<?php ActiveForm::end(); ?>
        </div>
    </div>
</div>
<?php
$css = <<<CSS
.checkbox{
    margin-top:0;
    margin-bottom: 0;
}
dl dt .checkbox-inline{
    font-weight: 700;
}
CSS;
$this->registerCss($css);
$js = <<<JS
$('dt input:checkbox').click(function(){
    if(this.checked){
        $(this).parents('dl').find('dd input:checkbox').each(function(){
            this.checked = true;
        })
    }else{
        $(this).parents('dl').find('dd input:checkbox').each(function(){
            this.checked = false;
        })
    }
})
$('dd.checkbox input').click(function(){
    if(this.checked){
        $(this).parents('dd').next('dd').find('input:checkbox').each(function(){
            this.checked = true;
        })
    }else{
        $(this).parents('dd').next('dd').find('input:checkbox').each(function(){
            this.checked = false;
        })
    }
})
JS;
$this->registerJs($js);
